package com.atguigu.gmall.oms.listener;

import com.atguigu.gmall.common.consts.GmallConsts;
import com.atguigu.gmall.oms.entity.OrderEntity;
import com.atguigu.gmall.oms.service.OrderService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rabbitmq.client.Channel;
import io.netty.util.concurrent.OrderedEventExecutor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@Configuration
public class OmsMqListener {
    @Resource
    OrderService orderService;

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "oms.queue2"),
                    exchange = @Exchange(value = GmallConsts.MQ_ORDER_EXCHANGE
                            ,type = ExchangeTypes.TOPIC),
                    key = "trade.success"
            )
    })
    public void paySuccess(Map<String,Object> map , Message message, Channel channel) throws IOException {
        try {
            String orderToken = map.get("orderToken").toString();
            Date paymentTime = (Date) map.get("paymentTime");
            Integer payType = Integer.parseInt(map.get("payType").toString());

            OrderEntity orderEntity = orderService.getOne(Wrappers.lambdaQuery(OrderEntity.class)
                    .eq(OrderEntity::getOrderSn, orderToken));
            if(orderEntity!=null && orderEntity.getStatus()==0){
                orderEntity.setPaymentTime(paymentTime);
                orderEntity.setPayType(payType);
                orderEntity.setStatus(1);//已支付
                orderService.updateById(orderEntity);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag() , false);
        } catch (Exception e) {
            //e.printStackTrace();
            if(message.getMessageProperties().isRedelivered()){
                //消息第二次消费失败: 丢弃消息
                channel.basicReject(message.getMessageProperties().getDeliveryTag() , false);
            }else{
                //消息第一次消费失败
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            }
        }
    }



    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "oms.queue"),
                    exchange = @Exchange(value = GmallConsts.MQ_ORDER_EXCHANGE
                            ,type = ExchangeTypes.TOPIC),
                    key = "oms.order.close"
            )
    })
    public void releaseStock(String orderToken , Message message, Channel channel) throws IOException {
        try {
            //关单
            OrderEntity orderEntity = orderService.getOne(Wrappers.lambdaQuery(OrderEntity.class)
                    .eq(OrderEntity::getOrderSn, orderToken));
            //【0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单】
            if(orderEntity!=null && orderEntity.getStatus()==0){
                //订单存在 并且仍然是待支付状态 关单
                orderEntity.setStatus(4);
                orderService.updateById(orderEntity);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag() , false);
        } catch (Exception e) {
            //e.printStackTrace();
            if(message.getMessageProperties().isRedelivered()){
                //消息第二次消费失败: 丢弃消息
                channel.basicReject(message.getMessageProperties().getDeliveryTag() , false);
            }else{
                //消息第一次消费失败
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            }
        }
    }

}
